package stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Spliterator;

/**
 * @author tuyou
 * @date 2020-08-19 21:45:31
 */
public class SpliteratorTest {

    @Test
    public void tryAdvance() {

        ArrayList list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);

        Spliterator spliterator = list.spliterator();
        System.out.println(spliterator.tryAdvance(System.out::println));
        System.out.println("------");
        System.out.println(spliterator.tryAdvance(System.out::println));
        System.out.println("------");
        System.out.println(spliterator.tryAdvance(System.out::println));
        System.out.println("------");
        System.out.println(spliterator.tryAdvance(System.out::println));
        System.out.println("------");
    }

    @Test
    public void trySplit() {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }

        Spliterator spliterator = list.spliterator();

        Spliterator fragment;
        while ((fragment = spliterator.trySplit()) != null) {
            printInfo(fragment);
        }

        printInfo(spliterator);
    }

    private void printInfo(Spliterator fragment) {
        System.out.println("-----------");
        System.out.println("size:" + fragment.estimateSize());
        fragment.forEachRemaining(System.out::println);
    }

    @Test
    public void hasCharacteristics() {
        ArrayList list = new ArrayList();
        list.add(1);
        list.add(3);
        list.add(2);

        Spliterator spliterator = list.spliterator();
        System.out.println(spliterator.hasCharacteristics(Spliterator.SIZED));
        System.out.println(spliterator.hasCharacteristics(Spliterator.CONCURRENT));
        System.out.println(spliterator.hasCharacteristics(Spliterator.DISTINCT));
        System.out.println(spliterator.hasCharacteristics(Spliterator.IMMUTABLE));
        System.out.println(spliterator.hasCharacteristics(Spliterator.NONNULL));
        System.out.println(spliterator.hasCharacteristics(Spliterator.ORDERED));
        System.out.println(spliterator.hasCharacteristics(Spliterator.SUBSIZED));
    }
}
